<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Banner extends CI_Controller {

	function __construct(){
		parent::__construct();
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
		}
		$this->load->model('admin/banner_model','banner_model');
	}
	
	public function index($id=0,$p=1)
	{
		$this->banner_list($id,$p);
	}
	
	public function banner_list($id=0,$p=1)
	{
		$menu['menu_1'] = '';
		$menu['menu_2'] = '';
		$menu['menu_3'] = '';
		$menu['menu_4'] = 'current';
		$menu['menu_5'] = '';
		
		$submenu['submenu_1'] = 'current';
		$submenu['submenu_2'] = '';
		$submenu['submenu_3'] = '';
		$submenu['submenu_4'] = '';
		$submenu['submenu_5'] = '';
		
		$lists = $this->banner_model->lists($id,1,500);
		$data['lists'] = array();
		$i = 0;
		foreach($lists as $v){
			$data['lists'][$i]['id'] = $v->id;
			$data['lists'][$i]['remark'] = $v->remark;
			$data['lists'][$i]['position'] = $this->banner_model->get_position($v->position_id);
			$data['lists'][$i]['status'] = $v->active?'active':'inactive';
			$data['lists'][$i]['date_start'] = date('d-m-Y',$v->date_start);
			$data['lists'][$i]['date_finish'] = date('d-m-Y',$v->date_finish);
			$i++;
		}
		
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view',$menu);
		$this->template->write_view('submenu','banner_submenu_view',$submenu);
		$this->template->write_view('content','banner_view',$data);
		$this->template->render();
	}
	
	public function add()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
			exit();
		}
		
		$menu['menu_1'] = '';
		$menu['menu_2'] = '';
		$menu['menu_3'] = '';
		$menu['menu_4'] = 'current';
		$menu['menu_5'] = '';
		
		$submenu['submenu_1'] = 'current';
		$submenu['submenu_2'] = '';
		$submenu['submenu_3'] = '';
		$submenu['submenu_4'] = '';
		$submenu['submenu_5'] = '';
		
		$data['banner_location'] = $this->banner_model->get_banner_location();
		
		$data['message'] = $this->session->flashdata('message');
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view',$menu);
		$this->template->write_view('submenu','banner_submenu_view',$submenu);
		$this->template->write_view('content','banner_add_view',$data);
		$this->template->render();
	}
	
	public function add_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
			exit();
		}
		
		if(isset($_POST['location']) AND $_POST['date_start']!='' AND isset($_POST['date_finish'])){
			$input['location'] = $_POST['location'];
			$input['date_start'] = strtotime($_POST['date_start']);
			$input['date_finish'] = strtotime($_POST['date_finish'].' 23:59:59');
			$input['url'] = ($_POST['url']);
			$input['code'] = ($_POST['code']);
			$input['used'] = $_POST['used'];
			$input['date_add'] = time();
			$input['remark'] = ($_POST['remark']);
			$id = $this->banner_model->add($input);
			
			if($id!=0){
				if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
					$this->load->library('ciupload');
					$this->ciupload->Upload($_FILES['img']);
					if ($this->ciupload->uploaded){
						//$this->ciupload->image_resize = true;
						//$this->ciupload->image_ratio_fill = true;
						//$this->ciupload->image_x = 156;
						//$this->ciupload->image_y = 100;
						$this->ciupload->Process(APPPATH.'../assets/image/banner/'.$id);
						
						if ($this->ciupload->processed){
							$input['id'] = $id;
							$input['img'] = $this->ciupload->file_dst_name;
							$result = $this->banner_model->update_img($input);
							if($result==0){
								@unlink(APPPATH.'../assets/image/banner/'.$id.'/'.$this->ciupload->file_dst_name);
								$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
							}else{
								redirect(base_url()."admin/banner");
							}
						}else{
							$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
							redirect(base_url()."admin/banner");
						}
					}else{
						$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
						redirect(base_url()."admin/banner");
					}
				}else{
					redirect(base_url()."admin/banner");
				}
			}else{
				$this->session->set_flashdata('message', 'ไม่สามารถบันทึกข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
				redirect(base_url()."admin/banner/add");
			}
		}else{
			$this->session->set_flashdata('message', 'กรุณากรอกข้อมูลให้ครบถ้วน');
			redirect(base_url()."admin/banner/add");
		}
	}
	
	public function edit($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		$menu['menu_1'] = '';
		$menu['menu_2'] = '';
		$menu['menu_3'] = '';
		$menu['menu_4'] = 'current';
		$menu['menu_5'] = '';
		
		$submenu['submenu_1'] = 'current';
		$submenu['submenu_2'] = '';
		$submenu['submenu_3'] = '';
		$submenu['submenu_4'] = '';
		$submenu['submenu_5'] = '';
		
		$data['message'] = $this->session->flashdata('message');
		
		$info = $this->banner_model->get_banner_info($id);
		$data['info']['id'] = $info[0]->id;
		$data['info']['remark'] = $info[0]->remark;
		if($info[0]->img!='' AND file_exists(APPPATH."../assets/image/banner/{$info[0]->id}/{$info[0]->img}")){
			$site_url = site_url();
			$data['info']['img'] = "<img src='{$site_url}assets/image/banner/{$info[0]->id}/{$info[0]->img}' />";
		}
		$data['info']['url'] = $info[0]->url;
		$data['info']['old_img'] = $info[0]->img;
		$data['info']['code'] = $info[0]->code;
		$data['info']['used_img'] = '';
		$data['info']['used_code'] = '';
		if($info[0]->used==1){
			$data['info']['used_img'] = 'checked="checked"';
		}else{
			$data['info']['used_code'] = 'checked="checked"';
		}
		$data['info']['date_start'] = date('Y-m-d',$info[0]->date_start);
		$data['info']['date_finish'] = date('Y-m-d',$info[0]->date_finish);
		
		$banner_location = $this->banner_model->get_banner_location();
		$data['banner_location'] = array();
		$i = 0;
		foreach($banner_location as $v){
			$data['banner_location'][$i]['id'] = $v->id;
			$data['banner_location'][$i]['name'] = $v->name;
			$data['banner_location'][$i]['position'] = $v->position;
			if($v->id==$info[0]->position_id){
				$data['banner_location'][$i]['selected'] = 'selected="selected"';
			}else{
				$data['banner_location'][$i]['selected'] = '';
			}
			$i++;
		}
		
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view',$menu);
		$this->template->write_view('submenu','banner_submenu_view',$submenu);
		$this->template->write_view('content','banner_edit_view',$data);
		$this->template->render();
	}
	
	public function edit_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(isset($_POST['location']) AND $_POST['date_start']!='' AND isset($_POST['date_finish'])){
			$input['location'] = $_POST['location'];
			$input['date_start'] = strtotime($_POST['date_start']);
			$input['date_finish'] = strtotime($_POST['date_finish'].' 23:59:59');
			$input['url'] = ($_POST['url']);
			$input['code'] = ($_POST['code']);
			$input['used'] = $_POST['used'];
			$input['remark'] = ($_POST['remark']);
			$input['id'] = ($_POST['id']);
			$input['date_modify'] = time();
			$check = $this->banner_model->update($input);
			
			if($check){
				if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
					$id = $_POST['id'];
					$this->load->library('ciupload');
					$this->ciupload->Upload($_FILES['img']);
					if ($this->ciupload->uploaded){
						//$this->ciupload->image_resize = true;
						//$this->ciupload->image_ratio_fill = true;
						//$this->ciupload->image_x = 156;
						//$this->ciupload->image_y = 100;
						$this->ciupload->Process(APPPATH.'../assets/image/banner/'.$id);
						
						if ($this->ciupload->processed){
							$input['id'] = $id;
							$input['img'] = $this->ciupload->file_dst_name;
							$old_img = $this->banner_model->get_banner_img($id);
							$result = $this->banner_model->update_img($input);
							if($result==0){
								@unlink(APPPATH.'../assets/image/banner/'.$id.'/'.$this->ciupload->file_dst_name);
								$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');;
				redirect(base_url()."admin/banner/edit/{$id}");
							}else{
								if($old_img->img!='' AND file_exists(APPPATH.'../assets/image/banner/'.$id.'/'.$old_img->img)){
									@unlink(APPPATH.'../assets/image/banner/'.$id.'/'.$old_img->img);
								}
								redirect(base_url()."admin/banner");
							}
						}else{
							$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
							redirect(base_url()."admin/banner/edit/{$id}");
						}
					}
				}else{
					redirect(base_url()."admin/banner");
				}
			}else{
				$this->session->set_flashdata('message', 'ไม่สามารถบันทึกข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
				redirect(base_url()."admin/banner/edit/{$id}");
			}
		}else{
			
			$this->session->set_flashdata('message', 'กรุณากรอกข้อมูลให้ครบถ้วน');
			redirect(base_url()."admin/banner/edit/{$_POST['id']}");
		}
	}
	
	public function status($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(($id+0)>0){
			$this->banner_model->status($id+0);
		}
		
		redirect(getenv("HTTP_REFERER"));
	}
	
	public function del($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(($id+0)>0){
			if($this->banner_model->delete($id+0)){
				$this->load->helper('devlism');
				devlism_remove_dir(APPPATH.'../assets/image/banner/'.$id);
			}
			redirect(base_url()."admin/banner");
		}else{
			redirect(base_url()."admin/banner");
		}
	}
	
	public function message()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
			exit();
		}
		
		$menu['menu_1'] = '';
		$menu['menu_2'] = '';
		$menu['menu_3'] = '';
		$menu['menu_4'] = 'current';
		$menu['menu_5'] = '';
		
		$submenu['submenu_1'] = '';
		$submenu['submenu_2'] = 'current';
		$submenu['submenu_3'] = '';
		$submenu['submenu_4'] = '';
		$submenu['submenu_5'] = '';
		
		$data['detail'] = htmlspecialchars_decode($this->banner_model->get_banner_message());
		
		$data['message'] = $this->session->flashdata('message');
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view',$menu);
		$this->template->write_view('submenu','banner_submenu_view',$submenu);
		$this->template->write_view('content','banner_message_view',$data);
		$this->template->render();
	}
	
	public function message_edit_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
			exit();
		}
		
		if(isset($_POST['detail'])){
			if($this->banner_model->update_banner_message(htmlspecialchars($_POST['detail']))){
				$this->session->set_flashdata('message', 'บันทึกข้อมูลเรียบร้อยแล้ว');
			}else{
				$this->session->set_flashdata('message', 'ไม่สามารถแก้ไขข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
			}
		}
		
		redirect(base_url()."admin/banner/message");
	}
}

/* End of file banner.php */
/* Location: ./modules/admin/controllers/banner.php */